/**
 * Copyright (C) 2017-2018 pinganfang, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.web.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Component;

import com.pingan.haofang.searchcloud.common.dto.PageDTO;
import com.pingan.haofang.searchcloud.common.rpc.RPCExporter;

/**
 * @author huhailiang937
 * @date 2017年5月31日下午7:54:15
 * @since 1.0.0
 */
@Component("houseDemoService")
public class HouseDemoService {

    /**
     * 
     * @param pageRquest
     * @return
     *
     */
    @RPCExporter("getHouseByPage")
    public PageDTO<House> getHouseByPage(PageRquest pageRquest) {
        final int total = 1050;
        final int pageSize = pageRquest.getPageSize();

        int totalPages = (total % pageSize == 0) ? total / pageSize : total / pageSize + 1;
        if (totalPages < pageRquest.getPageNo()) {
            PageDTO<House> housePage = PageDTO.<House> builder(Collections.emptyList()).totalPages(totalPages)
                    .totalRecords(total).pageNo(pageRquest.getPageNo()).pageSize(pageRquest.getPageSize()).build();
            return housePage;
        }

        int startId = pageRquest.getPageNo() * pageRquest.getPageSize();
        
        List<House> houses = new ArrayList<House>(pageSize);
        for(int id = startId; id< startId + pageRquest.getPageSize();id++){
            houses.add(this.getData(id));
        }
        
        PageDTO<House> housePage = PageDTO.<House> builder(houses).totalPages(totalPages)
                .totalRecords(total).pageNo(pageRquest.getPageNo()).pageSize(pageRquest.getPageSize()).build();
        
        return housePage;
    }

    /**
     * 
     * @param ids
     * @return
     *
     */
    @RPCExporter("getHouseByIds")
    public List<House> getHouseByIds(List<Integer> ids) {
        List<House> houses = new ArrayList<House>(ids.size());
        for (Integer id : ids) {
            houses.add(this.getData(id));
        }
        return houses;
    }

    private House getData(Integer id) {
        House house = new House();
        house.setId(id);
        house.setName("绿城万科皇家" + id + "号");
        house.setAddress("上海" + id + "号");
        house.setAreaId(id % 12 + 1);
        house.setCityId(id % 4 + 1);
        house.setPrice((id % 10 + 1) * 1000);
        house.setLocation_coordinate("3" + (id % 8 + 1) + ".850609,11" + (id % 7 + 1) + ".476141");
        house.setBelongUserIds(Arrays.asList(id, id + 1, id + 4, id + 9));
        house.setBelongUserNames(Arrays.asList("张" + id, "张" + (id + 1), "张" + (id + 4), "张" + (id + 9)));
        return house;
    }

    public class House {
        private Integer id;
        private Integer cityId;
        private Integer areaId;
        private String name;
        private String address;
        private Integer price;
        private String location_coordinate;
        private List<Integer> belongUserIds;
        private List<String> belongUserNames;

        /**
         * @return the id
         */
        public Integer getId() {
            return id;
        }

        /**
         * @param id
         *            the id to set
         */
        public void setId(Integer id) {
            this.id = id;
        }

        /**
         * @return the cityId
         */
        public Integer getCityId() {
            return cityId;
        }

        /**
         * @param cityId
         *            the cityId to set
         */
        public void setCityId(Integer cityId) {
            this.cityId = cityId;
        }

        /**
         * @return the areaId
         */
        public Integer getAreaId() {
            return areaId;
        }

        /**
         * @param areaId
         *            the areaId to set
         */
        public void setAreaId(Integer areaId) {
            this.areaId = areaId;
        }

        /**
         * @return the name
         */
        public String getName() {
            return name;
        }

        /**
         * @param name
         *            the name to set
         */
        public void setName(String name) {
            this.name = name;
        }

        /**
         * @return the address
         */
        public String getAddress() {
            return address;
        }

        /**
         * @param address
         *            the address to set
         */
        public void setAddress(String address) {
            this.address = address;
        }

        /**
         * @return the price
         */
        public Integer getPrice() {
            return price;
        }

        /**
         * @param price
         *            the price to set
         */
        public void setPrice(Integer price) {
            this.price = price;
        }

        /**
         * @return the location_coordinate
         */
        public String getLocation_coordinate() {
            return location_coordinate;
        }

        /**
         * @param location_coordinate
         *            the location_coordinate to set
         */
        public void setLocation_coordinate(String location_coordinate) {
            this.location_coordinate = location_coordinate;
        }

        /**
         * @return the belongUserIds
         */
        public List<Integer> getBelongUserIds() {
            return belongUserIds;
        }

        /**
         * @param belongUserIds
         *            the belongUserIds to set
         */
        public void setBelongUserIds(List<Integer> belongUserIds) {
            this.belongUserIds = belongUserIds;
        }

        /**
         * @return the belongUserNames
         */
        public List<String> getBelongUserNames() {
            return belongUserNames;
        }

        /**
         * @param belongUserNames
         *            the belongUserNames to set
         */
        public void setBelongUserNames(List<String> belongUserNames) {
            this.belongUserNames = belongUserNames;
        }
    }

}
